package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.Dept;
import org.example.pojo.Result;
import org.example.service.DeptService;
import org.springframework.web.bind.annotation.*;
/**
 * 部门管理
 */
import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController//标识是一个 请求处理 类y
public class DeptController {
    //日志
//    private static final org.slf4j.Logger log =
//            org.slf4j.LoggerFactory.getLogger(DeptController.class); 或者加注解@Slf4j
    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    //指定请求路径
//    @RequestMapping(value = "/depts", method = RequestMethod.GET)//默认是get请求，但可以指定请求方式、、
    @GetMapping//简化  ---Mapping
    public Result list(){
        log.info("查询所有部门");
       List<Dept> deptlist =deptService.findAll();
       return Result.success(deptlist);
    }
    @Log
    @DeleteMapping
    public Result delete( Integer id){
        log.info("根据ID删除部门:{}",id);
        deptService.deleteById(id);
        System.out.println(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        log.info("根据ID查询部门:{}",id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }


}
